import jwx from '@/common/jwx'
import config from "@/config/index.js"
export default function(callback) {
	let latitude = ''
	let longitude = ''
	uni.getLocation({
		type: 'wgs84', // 返回可以用于uni.openLocation的经纬度，默认为wgs84的gps坐标
		success: function(res) {
			latitude = res.latitude
			longitude = res.longitude
			console.log(latitude, longitude);
		},
		fail: function(error) {
			console.error('获取位置失败：', error);
		}
	});
	let promise;
	if (typeof callback !== "function") {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					// 都是gcj02坐标转换为百度坐标
					let wz = uni.$z.gps.gcj02tobd09(resolvedata.lng, resolvedata.lat);
					resolve({
						lng: wz[0],
						lat: wz[1]
					})
				} else {
					reject(rejectdata);
				}
			};
		});
	}
	//#ifndef H5
	// let wz = uni.$z.gps.wgs84togcj02(120.122726, 30.327005)
	// let wz = uni.$z.gps.wgs84togcj02(latitude, longitude)
	// callback({
	// 	lng: wz[0],
	// 	lat: wz[1]
	// })

	uni.getLocation({
		type: 'wgs84',

		success: function(res) {

			let wz = uni.$z.gps.wgs84togcj02(res.longitude, res.latitude)
			callback({
				lng: wz[0],
				lat: wz[1]
			})
		},
		fail: function() {
			callback(false)
		}
	});
	return promise
	//#endif
	// #ifdef H5
	if (config.islocalhost) {
		// let wz = uni.$z.gps.wgs84togcj02(120.122726, 30.327005)
		// let wz = uni.$z.gps.wgs84togcj02(latitude, longitude)
		// console.log(1111190909090);
		// callback({
		// 	lng: wz[0],
		// 	lat: wz[1]
		// })

		uni.getLocation({
			type: "wgs84",
			geocode: true,
			success: function(res) {
				console.log(res);
				let wz = uni.$z.gps.wgs84togcj02(60.560424, 56.864485)
				callback({
					lng: wz[0],
					lat: wz[1]
				})
			},
		});;
	} else if (uni.$z.IsWeixinOrAlipay() == "weixin" && jwx.isWechat()) {
		jwx.getlocation(function(res) {
			let position = {
				lng: res.longitude,
				lat: res.latitude
			};
			//alert(res.longitude+","+res.latitude)
			if (!res.longitude || !res.latitude) {
				uni.$u.toast(uni.$z.$t("未获取到定位请打开定位"))
				return
				position = {
					lng: 0,
					lat: 0
				}
			}
			callback(position)
		})
	} else {

		let geolocation = new qq.maps.Geolocation("J7GBZ-NPOWO-IJVWT-SNE7B-BTW2V-7KB5G", "电瓶车防盗系统");
		let options = {
			timeout: 20000
		};
		geolocation.getLocation(function(e) {
			callback(e);
		}, function() {

		}, options);

	}

	// #endif
	console.log(promise);
	return promise;
}